package com.example;

import org.springframework.core.io.ProtocolResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.File;

/**
 * @author linfeng
 * @date 2022/4/8 17:22
 */
public class MyProtocolResolver implements ProtocolResolver {

    private static final String URL_PREFIX = "userData:";

    @Override
    public Resource resolve(String location, ResourceLoader resourceLoader) {
        if(!location.startsWith(URL_PREFIX)){
            return null;
        }

        String fullyQualifiedURL = convertToFullyQualifiedURL(location);
        return resourceLoader.getResource(fullyQualifiedURL);
    }

    private String convertToFullyQualifiedURL(String location){
        return "file:" + location.replace(URL_PREFIX, System.getProperty("user.home") + File.separator);
    }
}
